package com.originskyseed.platform.user.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.user.i18n.UserResponseMsg;
import com.originskyseed.platform.user.model.SalaryCompose;
import com.originskyseed.platform.user.service.IDeptService;
import com.originskyseed.platform.user.service.ISalaryComposeService;
import com.originskyseed.platform.user.vo.request.SalaryComposeRequestVO;

/**
 * 薪酬构成信息Controller类（OSI_SALARY_COMPOSE）
 *
 * @author Tangwanghui
 */
@Controller
@RequestMapping("/salaryCompose")
@BaseLocaleResource(localeFileName = "i18n_user")
public class SalaryComposeController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(SalaryComposeController.class);

	@DubboReference
	private ISalaryComposeService salaryComposeService;

	@DubboReference
	private IDeptService deptService;

	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/salaryComposeList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<SalaryCompose>> salaryComposeList(HttpServletRequest request) {
		BaseResponseBody<BasePage<SalaryCompose>> result = null;
		try {
			// 请求对象
			SalaryComposeRequestVO requestVO = this.getParameterBean(SalaryComposeRequestVO.class);

			// 分页查询
			BasePage<SalaryCompose> page = new BasePage<SalaryCompose>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.salaryComposeService.selectListRelComByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<SalaryCompose>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<SalaryCompose>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/deptList.json", method = RequestMethod.POST)
	public BaseResponseBody deptList(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			Map<String, String> map = new HashMap<>();
			String name = this.getParameterString("name", null);
			String companyName = this.getParameterString("companyName", null);
			String setupName = this.getParameterString("setupName", null);
			map.put("name", name);
			map.put("companyName", companyName);
			map.put("setupName", setupName);

			// 查询
			List<Map<String, Object>> list = this.deptService.selectComposeList(map);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), list);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/salaryComposeSave.json", method = RequestMethod.POST)
	public BaseResponseBody<SalaryCompose> salaryComposeSave(HttpServletRequest request) {
		BaseResponseBody<SalaryCompose> result = null;
		try {
			// 请求对象
			SalaryCompose salaryCompose = this.getParameterBean(SalaryCompose.class);

			if (StringUtils.isNotBlank(salaryCompose.getId())) {
				this.salaryComposeService.updateByPrimaryKey(salaryCompose);
			} else {
				// 校验是否已经存在分公司数据
				if (salaryCompose.getCompanyId() != null) {
					SalaryCompose isNull = this.salaryComposeService.selectByCompanyId(salaryCompose.getCompanyId());
					if (isNull != null) {
						return new BaseResponseBody<SalaryCompose>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.COMPANY_DATA_EXIST.getMsg()), null);
					}
				}
				this.salaryComposeService.insertSelective(salaryCompose);
			}
			result = new BaseResponseBody<SalaryCompose>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), salaryCompose);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<SalaryCompose>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/salaryComposeDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> salaryComposeDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.salaryComposeService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

}
